﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;

namespace newTurtle8
{
    public class EnumUtils
    {
        public static string stringValueOf(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            HebrewNameAttribute[] attributes = (HebrewNameAttribute[])fi.GetCustomAttributes(typeof(HebrewNameAttribute), false);
            if (attributes.Length > 0)
                return attributes[0].Text;
            return value.ToString();

        }

        public static object enumValueOf(string value, Type enumType)
        {
            string[] names = Enum.GetNames(enumType);
            foreach (string name in names)
                if (stringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
                    return Enum.Parse(enumType, name);
            
            throw new ArgumentException("The string is not a description or value of the specified enum.");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns>an array containing all hebrew string describing the enum fields.</returns>
        public static string[] getHebDescriptionsArray(Type type)
        {
            if (!type.IsEnum)
                return null;
            List<string> list = new List<string>();
            //initialize comboBoxDiscStatus Items
            foreach (FieldInfo fi in type.GetFields())
            {
                HebrewNameAttribute[] names = (HebrewNameAttribute[])fi.GetCustomAttributes(typeof(HebrewNameAttribute), true);

                //only the custom attributes have more than one field.
                if (names.Length > 0)
                    list.Add(names[0].Text);
            }
            return list.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns>an array containing all hebrew string describing the enum fields.</returns>
        static public string getHebDescription(Enum en)
        {
            Type type = en.GetType();
            MemberInfo[] memInfo = type.GetMember(en.ToString());
            if (memInfo != null && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(HebrewNameAttribute), false);
                if (attrs != null && attrs.Length > 0)
                    return ((HebrewNameAttribute)attrs[0]).Text;
            }
            return "לא ידוע";
        }
       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e">self explanatory</param>
        /// <param name="text">the hebrew representation of the enum.</param>
        /// <returns>the string representing this enum. (English)</returns>
        static public string getEnumText(Type type, string heb_text)
        {
            if (!type.IsEnum)
                return null;
            Array enumVals = Enum.GetValues(type);

            foreach (var en in enumVals)
            {
                MemberInfo[] memInfo = type.GetMember(en.ToString());
                if (memInfo != null && memInfo.Length > 0)
                {
                    object[] attrs = memInfo[0].GetCustomAttributes(typeof(HebrewNameAttribute), false);
                    if (attrs != null && attrs.Length > 0 && ((HebrewNameAttribute)attrs[0]).Text.Equals(heb_text))
                        return en.ToString();
                }
            }
            return "Unknown";
        }
    }
}
